package com.wontube.smr.remote;

import java.util.List;

import com.wontube.client.extension.utils.StringUtils;
import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.core.annotation.CacheSupport;
import com.wontube.core.annotation.NoLoginSupport;
import com.wontube.core.pub.servlet.handlers.BasicHandler;
import com.wontube.smr.bo.sites.GenreSelectBO;
import com.wontube.smr.bo.sites.MovieSelectBO;
import com.wontube.smr.client.IMovieHandler;
import com.wontube.smr.client.model.po.Genre;
import com.wontube.smr.client.model.po.Movie;
import com.wontube.smr.client.model.vo.MovieItems;
import com.wontube.smr.dao.bo.MovieSelectDao;

/**
 * @author yuechao
 * 
 */
@NoLoginSupport
public final class MovieHandler extends BasicHandler implements IMovieHandler {
	@Override
	@CacheSupport(timeToLiveSeconds = 12 * 7 * 3600)
	public List<Genre> selectAllGenres() {
		return GenreSelectBO.selectRemandedMovieGenres();
	}
	
	@Override
	@CacheSupport
	public MovieItems search(String query, Double dbFactor, PagenateInfo pageInfo) {
		if (dbFactor == null) {
			return null;
		}

		return MovieSelectBO.searchHuluMovies(dbFactor.doubleValue(), query, pageInfo);
	}
	
	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public MovieItems selectRecent(Double dbFactor, PagenateInfo pageInfo) {
		return selectRecent(dbFactor, null, pageInfo);
	}
	
	@Override
	@CacheSupport(timeToLiveSeconds = 3600 * 6)
	public MovieItems selectRecent(Double dbFactor, String genreName, PagenateInfo pageInfo) {
		if (dbFactor == null) {
			return null;
		}

		//修正Others 等重复genre
		genreName = GenreSelectBO.joinMovieGenres(genreName);
		return MovieSelectBO.selectHuluRecent(genreName, dbFactor.doubleValue(), pageInfo);
	}
	
	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public MovieItems selectPopular(Double dbFactor, PagenateInfo pageInfo) {
		return selectPopular(dbFactor, null, pageInfo);
	}
	
	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public MovieItems selectPopular(Double dbFactor, String genreName, PagenateInfo pageInfo) {
		if (dbFactor == null) {
			return null;
		}
		
		//修正Others 等重复genre
		genreName = GenreSelectBO.joinMovieGenres(genreName);
		return MovieSelectBO.selectHuluPopular(genreName, dbFactor.doubleValue(), pageInfo);
	}

	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public MovieItems selectTopRated(Double dbFactor, PagenateInfo pageInfo) {
		return selectTopRated(dbFactor, null, pageInfo);
	}

	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public MovieItems selectTopRated(Double dbFactor, String genreName, PagenateInfo pageInfo) {
		if (dbFactor == null) {
			return new MovieItems();
		}
		
		//修正Others 等重复genre
		genreName = GenreSelectBO.joinMovieGenres(genreName);
		return MovieSelectBO.selectHuluTopRated(dbFactor.doubleValue(), genreName, pageInfo);
	}

	@Override
	@CacheSupport
	public Movie getMovie(String movieId) {
		if(StringUtils.isNullOrEmpty(movieId) || "undefined".equals(movieId)){
			return null;
		}		
		
		return MovieSelectBO.getMovie(movieId);
	}

	@Override
	@CacheSupport
	public MovieItems searchTrailers(String query, PagenateInfo pageInfo) {
		return MovieSelectDao.search(query, Movie.TYPE_TRAILER, pageInfo);
	}
	
	@Override
	@CacheSupport
	public MovieItems selectTrailers(PagenateInfo pageInfo) {
		return selectTrailers(null, pageInfo);
	}

	@Override
	@CacheSupport
	public MovieItems selectTrailers(String genreName, PagenateInfo pageInfo) {
		//修正Others 等重复genre
		genreName = GenreSelectBO.joinMovieGenres(genreName);
		return MovieSelectDao.selectMovieTraillers(genreName, pageInfo);
	}

	@Override
	@CacheSupport(timeToLiveSeconds = 2 * 3600)
	public MovieItems selectComingSoonTrailers(String genreName, PagenateInfo pageInfo) {
		return MovieSelectDao.selectComingSoonTrailers(genreName, pageInfo);
	}

	@Override
	@CacheSupport(timeToLiveSeconds = 2 * 3600)
	public MovieItems selectInTheaterTrailers(String genreName, PagenateInfo pageInfo) {
		return MovieSelectDao.selectInTheaterTrailers(genreName, pageInfo);
	}
}
